﻿#region using
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Windows.Threading;
using Microsoft.Surface.Presentation;
using Microsoft.Surface.Presentation.Controls;
using Microsoft.Surface.Presentation.Input;
using System.Xml;
using System.IO;
using ICSharpCode.SharpZipLib.Tar;
using ICSharpCode.SharpZipLib.GZip;
#endregion

namespace MultiTouchDemonstrator
{
    class SaveProject
    {

        public static void AddDirectoryFilesToTar(TarArchive tarArchive, string sourceDirectory, bool recurse)
        {
            // Recursively add sub-folders
            if (recurse)
            {
                string[] directories = Directory.GetDirectories(sourceDirectory);
                foreach (string directory in directories)
                    AddDirectoryFilesToTar(tarArchive, directory, recurse);
            }

            // Add files
            string[] filenames = Directory.GetFiles(sourceDirectory);
            foreach (string filename in filenames)
            {
                TarEntry tarEntry = TarEntry.CreateEntryFromFile(filename);
                tarArchive.WriteEntry(tarEntry, true);
            }
        }
        public static void addFileToTar(TarArchive tarArchive, string fileName)
        {
            TarEntry tarEntry = TarEntry.CreateEntryFromFile(fileName);
            tarArchive.WriteEntry(tarEntry, true);
        }
        public static void save(String path, MapArea mapArea)
        {

            System.IO.FileInfo fileInfo = new System.IO.FileInfo(path + @"\save.xml");
            //fileInfo.Delete();
            System.IO.FileStream sw = new System.IO.FileStream(path + @"\save.xml", System.IO.FileMode.Create, System.IO.FileAccess.Write);
            sw.Flush();
            Matrix mapMatrix = ((MatrixTransform)mapArea.MapCanvas.RenderTransform).Matrix;

            if (mapMatrix != null)
            {
                double m11 = mapMatrix.M11;
                double m22 = mapMatrix.M22;
                double m12 = mapMatrix.M12;
                double m21 = mapMatrix.M21;
                double offsetX = mapMatrix.OffsetX;
                double offsetY = mapMatrix.OffsetY;
                XmlTextWriter saveFile = new XmlTextWriter(sw, System.Text.Encoding.UTF8);
                saveFile.WriteStartDocument();
                saveFile.WriteWhitespace("\n");
                saveFile.WriteComment("sauvgarde d'un projet DMT");
                saveFile.WriteWhitespace("\n");
                saveFile.WriteStartElement("dmt");
                saveFile.WriteWhitespace("\n");
                //map
                saveFile.WriteStartElement("map");
                saveFile.WriteAttributeString("id", "map");
                saveFile.WriteAttributeString("m11", m11.ToString());
                saveFile.WriteAttributeString("m12", m12.ToString());
                saveFile.WriteAttributeString("m21", m21.ToString());
                saveFile.WriteAttributeString("m22", m22.ToString());
                saveFile.WriteAttributeString("offsetX", offsetX.ToString());
                saveFile.WriteAttributeString("offsetY", offsetY.ToString());
                //saveFile.WriteAttributeString("origineX", origine.X.ToString());
                //saveFile.WriteAttributeString("origineY", origine.Y.ToString());
                saveFile.WriteAttributeString("zoomX", mapArea.ZoomCanvas.X.ToString());
                saveFile.WriteAttributeString("zoomY", mapArea.ZoomCanvas.Y.ToString());
                saveFile.WriteEndElement();
                saveFile.WriteWhitespace("\n");

                foreach (UIElement element in mapArea.MapCanvas.Children)
                {
                    if (element is Image)
                    {
                        Image item = element as Image;
                        double x = 0;
                        double y = 0;

                        if (item != null && item.Tag != null)
                        {
                            Image image = item as Image;
                            TransformGroup rectsMatrix = ((TransformGroup)image.RenderTransform);
                            foreach (var transform in rectsMatrix.Children)
                            {
                                if (transform is TranslateTransform)
                                {
                                    TranslateTransform translate = (TranslateTransform)transform;
                                    x = translate.X;
                                    y = translate.Y;
                                }
                            }
                            BitmapImage source = image.Source as BitmapImage;
                            string id = System.IO.Path.GetFileName(source.UriSource.LocalPath);
                            saveFile.WriteStartElement("item");
                            saveFile.WriteAttributeString("id", id);
                            saveFile.WriteAttributeString("X", x.ToString());
                            saveFile.WriteAttributeString("Y", y.ToString());
                            saveFile.WriteEndElement();
                            saveFile.WriteWhitespace("\n");


                        }
                    }
                }
                saveFile.WriteWhitespace("\n");
                saveFile.WriteEndElement();
                saveFile.Flush();
                saveFile.Close();



            }
        }
        public static bool createDMTFile(string DirectoryName, string projectName, string symbolDirectoryPath, string mapPath, MapArea mapArea)
        {
            System.IO.FileStream fs = new System.IO.FileStream(DirectoryName + @"\" + projectName + ".dmt", System.IO.FileMode.Create, System.IO.FileAccess.Write);
            Stream gzipStream = new GZipOutputStream(fs);
            TarArchive tarArchive = TarArchive.CreateOutputTarArchive(gzipStream);
            string current = Directory.GetParent(symbolDirectoryPath).ToString();
            Directory.SetCurrentDirectory(current);
            string imageFolder = new System.IO.DirectoryInfo(symbolDirectoryPath).Name;
            AddDirectoryFilesToTar(tarArchive, imageFolder, true);
            save(DirectoryName,mapArea);
            Directory.SetCurrentDirectory(DirectoryName);
            addFileToTar(tarArchive, "save.xml");
            string mydocpath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
            current = Directory.GetParent(mapPath).ToString();
            Directory.SetCurrentDirectory(current);
            addFileToTar(tarArchive, System.IO.Path.GetFileName(mapPath));
            tarArchive.Close();
            fs.Close();
            return true;
        }
    }
}
